package org.bigk.invoices.services;

import org.apache.commons.lang.StringUtils;
import org.bigk.invoices.model.Invoice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * SNY - Short Number and then Year. What I mean is human readable format:
 * Number/Year.
 * 
 * @author lukasz.kalek
 * 
 */
@Service("hrInvoiceNumberService")
@Transactional
public class HRInvoiceNumberServiceSNYImpl implements HRInvoiceNumberService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(HRInvoiceNumberServiceSNYImpl.class);

	@Override
	public boolean updateHRInvoiceNumber(Invoice invoice) {
		if (logger.isDebugEnabled()) {
			logger.debug("updateHRInvoiceNumber(Invoice invoice=" + invoice
					+ ") - start");
		}
		
		if (StringUtils.isEmpty(invoice.getYear())) {
			if (logger.isDebugEnabled()) {
				logger.debug("updateHRInvoiceNumber(Invoice) - end - return value=" + false);
			}
			return false;
		}
		
		if (invoice.getNumber() == null) {
			if (logger.isDebugEnabled()) {
				logger.debug("updateHRInvoiceNumber(Invoice) - end - return value=" + false);
			}
			return false;
		}
		
		String hrNo = invoice.getNumber() + "/" + invoice.getYear();
		invoice.setHrInvoiceNumber(hrNo);
		
		if (logger.isDebugEnabled()) {
			logger.debug("updateHRInvoiceNumber(Invoice) - end - return value=" + true);
		}
		return true;
	}
	
}
